home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12403 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  57 lines

  1. Path: thales.nmia.com!not-for-mail
  2. From: ghealton@nmia.com (Gilbert Healton)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Output to file vs. cout
  5. Date: 19 Mar 1996 21:02:29 GMT
  6. Organization: New Mexico Internet Access
  7. Message-ID: <4in7d5$9el@thales.nmia.com>
  8. References: <JL.96Mar7165152@thyme.id.dth.dk> <4i18mg$ba5@hpbblb.bbn.hp.com> <4impgh$jnl@is-news.gov.ab.ca>
  9. NNTP-Posting-Host: socrates.nmia.com
  10. X-Newsreader: TIN [UNIX 1.3 950515BETA PL0]
  11.  
  12. Ron Orr (orr@lisd.env.gov.ab.ca) wrote:
  13. : Matthias Dittrich (matti) wrote:
  14. : : jl@id.dth.dk (J°rn Lind-Nielsen) wrote:
  15. : : >
  16. : : >Here's another of the probs that comes from porting C to C++:
  17. : : >
  18. : : >- I want to be able to redirect program output to either a file or cout.
  19. : : >  Typically this would be selected by a commandline option ("-o outfile").
  20. : : >
  21. : : >- In C I would do something like this:
  22. : : >
  23. : : >
  24. : : >     main()
  25. : : >     {
  26. : : >       FILE *outputfile;
  27. : : >
  28. : : >       if (commandline == do_output_to_file)
  29. : : >          outputfile = stdout;
  30. : : >       else
  31. : : >          outputfile = fopen(passed_filename, "w");
  32. : : >
  33. : : >       fprintf(outfile, "Hello world\n");
  34. : : >
  35. : : >       fclose(outfile);  /* Maybe - not allways necassary */
  36. : : >     }
  37. : : >
  38. : : >- The question is:  How is this done in C++ ?
  39. : : >
  40. : : >.......
  41. : : ofstream outfile(passed_filename); /* this constructor opens your file */
  42. : : outfile << "Hello world" << endl;  /* writes the string */
  43. : : The destructor closes the file and of course you should do some error checking.
  44.  
  45. I, too, am looking for a solution to this. It seems it is taking me longer
  46. to get all the ins and outs of streams than the C++ language..
  47.  
  48. The ability to have a stream that you open, and close/reopen, at various points
  49. of the file is also of interest.
  50.  
  51. -- 
  52. ----- Computer Consulting / Web Pages -----    http://www.nmia.com/~ghealton/
  53. These opinions are my own. Life is learning and I may retract, modify, 
  54. even attack, my previous ideas at any time without notice.
  55.